/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.polimi.registration.gui.security;

import it.polimi.registration.business.security.boundary.EventManager;
import it.polimi.registration.business.security.boundary.InviteManager;
import it.polimi.registration.business.security.boundary.WeatherService;
import it.polimi.registration.business.security.entity.Event;
import it.polimi.registration.business.security.entity.Invite;
import it.polimi.registration.business.security.entity.InvitePK;
import it.polimi.registration.business.security.entity.User;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Objects;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;



/**
 *
 * @author Stefano
 */
@Named
@RequestScoped
public class EventBean {

    @PersistenceContext 
    private EntityManager e;
    
    @EJB
    private EventManager em;
    
    @EJB
    private InviteManager im;
    
    private Event event;
    
    @EJB
    private NotificationBean nb;
    
    @EJB
    private WeatherService ws;

   
    
    public EventBean() {      
        
    }
    
    public Event getEvent() {
        if (event == null) {
            event = new Event();
        }
        return event;
    }
    
    public void setEvent(Event event) {
        this.event = event;
    }
    
    public void save() {
        em.save(event);
    }
    
    public List<Event> findAllEvent(User user){
        TypedQuery<Event> query = e.createQuery("SELECT e FROM Event e Where e.creator = :mail", Event.class).setParameter("mail", e.find(User.class, user.getEmail()));
    return query.getResultList();
    }
    
    public void moveEvent(String id, int day, int minutes) throws IOException{
        Event evento = em.getEvent(id);
        if(evento.getCreator().getEmail()==em.getLoggedUser().getEmail())
        {
            evento.getStartTime().add(Calendar.DAY_OF_YEAR, day);
            evento.getStartTime().add(Calendar.MINUTE, minutes);
            evento.getEndTime().add(Calendar.DAY_OF_YEAR, day);
            evento.getEndTime().add(Calendar.MINUTE, minutes);
            List<User> participants = new ArrayList();
            participants = im.findInvited(evento);
                    if (participants != null) {
                        if (!participants.isEmpty()) {
                            for (User user : participants) {
                                List<Invite> invites = im.findInvites(user, evento);
                                for (Invite invite : invites) {
                                    im.delete(invite);
                                }
                                InvitePK invitePK = new InvitePK(user.getEmail(), evento.getId());
                                Invite i = new Invite(invitePK);
                                i.setEvent(evento);
                                i.setUser(user);
                                im.save(i);
                            }
                        }
                    }
        }
        em.merge(evento);
    }
    
    public void resizeEvent(String id, int day, int minutes){
        Event evento = em.getEvent(id);
        if(evento.getCreator().equals(em.getLoggedUser())){
            evento.getEndTime().add(Calendar.DAY_OF_YEAR, day);
            evento.getEndTime().add(Calendar.MINUTE, minutes);
            em.merge(evento);
        }
    }
    
    public void reschedule(Event e,Calendar c,int value)
    {
        em.modifyDate(e, c, value);
        nb.lettaNotificaMeteo(e);
    }
   
    public void leggi(Event e)
    {
        nb.lettaNotificaMeteo(e);
    }
}
